// Copyright 2013 Ronald Schlenker, www.technewlogic.de.
// 
// This file is part of Byond.
// 
// Byond is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
// 
// Byond is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with Byond.  If not, see <http://www.gnu.org/licenses/>.

using System.Windows.Input;
using Byond.EnsembleLibrary;
using Byond.Frontend.Messages;
using Mvvm;
using Byond.Messages;
using Utils;
using Utils.Messaging;

namespace Byond.Frontend.EnsembleBrowserNew.ViewModels
{
	public class ItemViewModel : ViewModelBase
	{
		private readonly LibraryItem _libraryItem;
		private readonly FolderViewModel _parent;
		private readonly EnsembleAdministrationWorkflows _ensembleAdministrationWorkflows;
		private readonly ItemSelectionArbiter _itemSelectionArbiter;

		public ItemViewModel(LibraryItem libraryItem, FolderViewModel parent)
		{
			_libraryItem = libraryItem;
			_parent = parent;
			ShowCommand = new RelayCommand(Show);
		}

		private void Ctor()
		{
			_itemSelectionArbiter.Register(this);
		}

		public event MessageEventHandler SendMessage;

		public ICommand ShowCommand { get; private set; }

		public string Name { get { return _libraryItem.Name; } }
		public string FileName { get { return _libraryItem.Path; } }

		private bool _isSelected;
		public bool IsSelected
		{
			get { return _isSelected; }
			set
			{
				_isSelected = value;
				OnPropertyChanged("IsSelected");
			}
		}

		public void Show()
		{
			SendMessage(new EditDocumentRequest(_libraryItem.Path));
			_itemSelectionArbiter.Select(FileName);
		}

		public void ExpandTreeAndSelect()
		{
			_parent.ExpandTree();
			_itemSelectionArbiter.Select(FileName);
		}

		public void Delete()
		{
			_ensembleAdministrationWorkflows.Delete(FileName);
		}
	}
}
